package Web.Servlet;

import Domain.Cart;
import Domain.Product;
import Domain.User;
import Service.CartService;
import Service.Impl.CartServiceImpl;
import Service.Impl.ProductServiceImpl;
import Service.Impl.UserServiceImpl;
import Service.ProductService;
import Service.UserService;
import com.sun.xml.bind.v2.model.core.ID;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/CartServlet")
public class CartServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String action = request.getParameter("action");

        if(action.equals("IntoCart")){
            String id = request.getParameter("id");


            UserService service = new UserServiceImpl();
            List<User> list = service.findUserById(Integer.parseInt(id));
            User user = list.get(0);

            CartService service1 = new CartServiceImpl();
            List<Cart> list1 = service1.FindCartByUserid(Integer.parseInt(id));
            request.setAttribute("user",user);
            request.setAttribute("product",list1);

            request.getRequestDispatcher("Cart/CartPage.jsp").forward(request,response);
        }

        if(action.equals("DelCartItem")){

            String uid = request.getParameter("uid");
            String pid = request.getParameter("pid");

            CartService service = new CartServiceImpl();
            service.DelCartItem(Integer.parseInt(pid));
            List<Cart> carts = service.FindCartByUserid(Integer.parseInt(uid));

            UserService service1 = new UserServiceImpl();
            List<User> list = service1.findUserById(Integer.parseInt(uid));
            User user = list.get(0);

            request.setAttribute("user",user);
            request.setAttribute("product",carts);

            request.getRequestDispatcher("Cart/CartPage.jsp").forward(request,response);

        }

        if(action.equals("AddCart")){
            String uid = request.getParameter("uid");
            String pid = request.getParameter("pid");

            ProductService service = new ProductServiceImpl();
            List<Product> list = service.FindProductById(Integer.parseInt(pid));
            Product product = list.get(0);
            double price = product.getPrice();

            CartService service1 = new CartServiceImpl();
            service1.AddCart(Integer.parseInt(uid),Integer.parseInt(pid),price);

            if(!uid.equals("")){
                UserService service2 = new UserServiceImpl();
                List<User> list1 = service2.findUserById(Integer.parseInt(uid));
                User user = list1.get(0);
                request.setAttribute("user",user);
            }

            request.setAttribute("product",product);
            request.getRequestDispatcher("Product/Details.jsp").forward(request,response);


        }

        if(action.equals("AddNum")){
            String id = request.getParameter("id");
            String uid = request.getParameter("uid");

            CartService service = new CartServiceImpl();
            List<Cart> list = service.FindCartItemById(Integer.parseInt(id));
            Cart cart = list.get(0);

            int cnum = cart.getCnum();//该购物车项选定得商品数量

            ProductService service1 = new ProductServiceImpl();
            List<Product> list1 = service1.FindProductById(cart.getProductid());
            Product product = list1.get(0);
            int num = product.getNum();//当前商品的库存数量

            //计算数量
            if(cnum>=num){
                cnum = num;
            }else {
                cnum++;
            }

            Double total = cnum*product.getPrice();
            service.UpdateCartItemNumById(Integer.parseInt(id),cnum,total);//修改数量

            response.sendRedirect(request.getContextPath()+"/CartServlet?action=IntoCart&id="+uid);
        }

        if(action.equals("ReduceNum")){
            String id = request.getParameter("id");
            String uid = request.getParameter("uid");

            CartService service = new CartServiceImpl();
            List<Cart> list = service.FindCartItemById(Integer.parseInt(id));
            Cart cart = list.get(0);
            int cnum = cart.getCnum();//该购物车项选定得商品数量

            ProductService service1 = new ProductServiceImpl();
            List<Product> list1 = service1.FindProductById(cart.getProductid());
            Product product = list1.get(0);
            int num = product.getNum();//当前商品的库存数量

            //计算数量
            if(cnum<=1){
                cnum = 1;
            }else {
                cnum--;
            }
            Double total = cnum*product.getPrice();
            service.UpdateCartItemNumById(Integer.parseInt(id),cnum,total);//修改数量
            response.sendRedirect(request.getContextPath()+"/CartServlet?action=IntoCart&id="+uid);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
